using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using PdfLib;

namespace PdfLib.Test.PdfParser
{
    [ManualTestCase("Text reader", "PDF parser")]
    public partial class TestCase_TextReader : UserControl
    {
        public TestCase_TextReader()
        {
            InitializeComponent();
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Filter = "PDF files(*.pdf)|*.pdf|All files(*.*)|*.*";
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_PdfPath.Text = dlg.FileName;
                int pageCount = 1;
                //if(this.ck_UseTetUtil.Checked)
                //{
                //    pageCount = PdfTetUtil.GetPageCount(dlg.FileName);
                //}
                //else
                //{
                    
                //}
                pageCount = PdfPropertyReader.GetPageCount(this.txt_PdfPath.Text);
                this.num_Page.Value = 1;
                this.num_Page.Maximum = pageCount;
            }
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
        }

        private void OpenPage(string pdfFilePath, int pageNum)
        {
            
            try
            {
                List<PdfTextToken> txtTokens = null;
                //if(this.ck_UseTetUtil.Checked)
                //{
                //    txtTokens = PdfTetUtil.ReadPageContentAsTextToken(pdfFilePath, pageNum);
                //}
                //else
                //{
                //    txtTokens = PdfTextReader.ReadPageContentAsTokens(pdfFilePath, pageNum);
                //}
                txtTokens = PdfTextReader.ReadPageContentAsTokens(pdfFilePath, pageNum);
                this.dv_TxtTokens.Rows.Clear();
                txtTokens.Sort();
                foreach (PdfTextToken txtToken in txtTokens)
                {
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellTxt = new DataGridViewTextBoxCell();
                    cellTxt.Value = txtToken.Text;
                    dr.Cells.Add(cellTxt);
                    DataGridViewTextBoxCell cellX = new DataGridViewTextBoxCell();
                    cellX.Value = txtToken.TextRect.X;
                    dr.Cells.Add(cellX);
                    DataGridViewTextBoxCell cellY = new DataGridViewTextBoxCell();
                    cellY.Value = txtToken.TextRect.Y;
                    dr.Cells.Add(cellY);
                    DataGridViewTextBoxCell cellW = new DataGridViewTextBoxCell();
                    cellW.Value = txtToken.TextRect.Width;
                    dr.Cells.Add(cellW);
                    DataGridViewTextBoxCell cellH = new DataGridViewTextBoxCell();
                    cellH.Value = txtToken.TextRect.Height;
                    dr.Cells.Add(cellH);
                    DataGridViewTextBoxCell cellFont = new DataGridViewTextBoxCell();
                    cellFont.Value = txtToken.FontName;
                    dr.Cells.Add(cellFont);
                    DataGridViewTextBoxCell cellSize = new DataGridViewTextBoxCell();
                    cellSize.Value = txtToken.FontSize;
                    dr.Cells.Add(cellSize);
                    this.dv_TxtTokens.Rows.Add(dr);
                }
                List<PdfTextLine> lineTokens = PdfTextReader.CreateLines(txtTokens);
                lineTokens = PdfTextReader.MergeLines(lineTokens, PdfTextReader.LineSpacing);
                this.dv_LineTokens.Rows.Clear();
                int lineNum = 0;
                foreach (PdfTextLine lineToken in lineTokens)
                {
                    lineNum++;
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellLineNum = new DataGridViewTextBoxCell();
                    cellLineNum.Value = lineNum;
                    dr.Cells.Add(cellLineNum);
                    DataGridViewTextBoxCell cellLineText = new DataGridViewTextBoxCell();
                    cellLineText.Value = lineToken.LineContent;
                    dr.Cells.Add(cellLineText);
                    this.dv_LineTokens.Rows.Add(dr);
                }

                string pageContent = PdfTextReader.ReadPageContent(pdfFilePath, pageNum);
                this.rt_PageContent.Text = pageContent;

                Image pageImg = PdfTextReader.GetPageTextLayout(pdfFilePath, pageNum);
                this.pic_TxtLayout.Image = pageImg;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}
